package com.isnmp.nms.framework.discover.service;

import java.util.Iterator;
import java.util.List;

import com.isnmp.common.snmp.SNMPFactory;
import com.isnmp.common.snmp.SNMPTarget;
import com.isnmp.common.snmp.mib.MibSoftwareRunEntry;
import com.isnmp.nms.framework.discover.DeviceInfo;
import com.isnmp.nms.framework.discover.DeviceTypeInfo;
import com.isnmp.nms.framework.discover.DiscoverConfig;

/**
 * 
 * <p>
 * Description: 此类用来匹配一个SNMP设备的类型
 * </p>
 * 
 *router    -cicso                      -1.3.6.1.2.1.17.2.1.0：无   
 *                                       1.3.6.1.2.1.4.1.0：1     
 *             
 *          -Ruijie                     -1.3.6.1.2.1.17.2.1.0：无   
 *                                       1.3.6.1.2.1.4.1.0：1
 *                                       
 *          -mypower                    ...
 *                               
 *firewall  -cicso                      -1.3.6.1.2.1.17.2.1.0：无   
 *                                       1.3.6.1.2.1.4.1.0：1
 *                                       
 *          -netscreen                  -1.3.6.1.2.1.17.2.1.0：无   
 *                                       1.3.6.1.2.1.4.1.0：2
 *                                     s
 *l3swith   -cicso                      -1.3.6.1.2.1.17.2.1.0：无   
 *                                       1.3.6.1.2.1.4.1.0：1               
 *                                        
 *          -juniper                    -1.3.6.1.2.1.17.2.1.0：3      
 *                                       1.3.6.1.2.1.4.1.0：1      
 *
 * 
 *host      -windows                    -1.3.6.1.2.1.17.2.1.0：无
 *                                       1.3.6.1.2.1.4.1.0：2
 *
 *          -
 * 
 *
 * @author wangyazhou
 * @version 1.0
 * @since 
 * @see
 */
public class SearchTypeService {
    /**
     * 匹配一个设备的类型
     * @param paramDeviceInfo
     * @return
     */
    @SuppressWarnings("rawtypes")
    public DeviceTypeInfo matchDeviceType(DeviceInfo paramDeviceInfo) {
        List localList1 = DiscoverConfig.getAllDeviceType();
        Object localObject2 = localList1.iterator();
        Object localObject1;
        Iterator localIterator;
        String str;
        while (((Iterator) localObject2).hasNext()) {
            localObject1 = (DeviceTypeInfo) ((Iterator) localObject2).next();
            localIterator = ((DeviceTypeInfo) localObject1).getTypeOIDCharacter().iterator();
            while (localIterator.hasNext()) {
                str = (String) localIterator.next();
                if ((!str.equals("")) && (paramDeviceInfo.getDeviceSysOID().indexOf(str) >= 0))
                    return (DeviceTypeInfo) localObject1;
            }
        }
        localObject2 = localList1.iterator();
        while (((Iterator) localObject2).hasNext()) {
            localObject1 = (DeviceTypeInfo) ((Iterator) localObject2).next();
            localIterator = ((DeviceTypeInfo) localObject1).getTypeDescCharacter().iterator();
            while (localIterator.hasNext()) {
                str = (String) localIterator.next();
                if ((!str.equals("")) && (paramDeviceInfo.getDeviceDesc().toLowerCase().indexOf(str.toLowerCase()) >= 0))
                    return (DeviceTypeInfo) localObject1;
            }
        }
        if ((paramDeviceInfo.getDeviceProtocol().equalsIgnoreCase("SNMP")) && (paramDeviceInfo.getSnmpTarget() != null)) {
            localObject1 = paramDeviceInfo.getSnmpTarget();
            localObject2 = null;
            str = null;

            /**
             * 1:acting as a gateway  2:not-acting as a gateway
             */
            try {
                localObject2 = SNMPFactory.getSNMPAPI().getOIDValue(".1.3.6.1.2.1.4.1.0", (SNMPTarget) localObject1);
            } catch (Exception localException1) {
                System.out.println(paramDeviceInfo.getDeviceIP() + Messages.getString("SearchTypeService.3") + localException1.getMessage());
            }

            /**
             * 
             */
            try {
                str = SNMPFactory.getSNMPAPI().getOIDValue(".1.3.6.1.2.1.17.2.1.0", (SNMPTarget) localObject1);
            } catch (Exception localException2) {
                System.out.println(paramDeviceInfo.getDeviceIP() + Messages.getString("SearchTypeService.5") + localException2.getMessage());
            }

            //
            if ((str != null) && (!str.equalsIgnoreCase("noSuchObject")) && (!str.equalsIgnoreCase("noSuchInstance"))) {
                if ((localObject2 != null) && (((String) localObject2).equals("1"))) {
                    return DeviceTypeInfo.RouterSwitchType;
                }
                return DeviceTypeInfo.SwitchType;
            }

            //
            if ((localObject2 != null)) {
                if (((String) localObject2).equals("1")) {
                    return DeviceTypeInfo.RouterType;
                } else if (((String) localObject2).equals("2")) {
                    return DeviceTypeInfo.ComputerType;
                }
            }

            try {
                List localList2 = SNMPFactory.getSNMPAPI().getAllTableData(MibSoftwareRunEntry.class, (SNMPTarget) localObject1);
                if (localList2.size() > 0) {
                    return DeviceTypeInfo.ComputerType;
                }
            } catch (Exception localException3) {
            }
        }
        return DeviceTypeInfo.UnkownType;
    }
}